package cn.jobstart.service.impl;

import cn.jobstart.common.*;
import cn.jobstart.dao.ISpiderMonitorDao;
import cn.jobstart.enums.SpiderMonitorStatusEnum;
import cn.jobstart.enums.TalentSubscriptionTaskStatusEnum;
import cn.jobstart.model.TalentSubscriptionTask;
import cn.jobstart.modelExt.SpiderMonitorExt;
import cn.jobstart.modelExt.TalentSubscriptionTaskExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.SpiderMonitorSearchExt;
import cn.jobstart.service.IAdminService;
import cn.jobstart.service.IResumeBasicService;
import cn.jobstart.service.ISpiderMonitorService;
import cn.jobstart.service.ITalentSubscriptionTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author sunke
 * @Date 2023-09-20 10:25:36
 * @description SpiderMonitorService      爬虫监控
 */
@Service(value = "spiderMonitorService")
@Slf4j
public class SpiderMonitorServiceImpl extends AbstractBaseService<SpiderMonitorExt, SpiderMonitorSearchExt, String> implements ISpiderMonitorService {

    @Resource(name = "spiderMonitorDao")
    private ISpiderMonitorDao spiderMonitorDao;

    @Resource
    private ITalentSubscriptionTaskService talentSubscriptionTaskService;

    @Resource
    private IResumeBasicService resumeBasicService;

    @Resource
    private IAdminService adminService;

    @Override
    public IBaseDao<SpiderMonitorExt, SpiderMonitorSearchExt, String> getDao() {
        return spiderMonitorDao;
    }


    /**
     * 从订单任务创建对象
     *
     * @param task
     * @eturn
     */
    @Override
    public SpiderMonitorExt createByTask(TalentSubscriptionTaskExt task) {
        if(StringUtil.isNullOrEmpty(task)){
            return null;
        }
        SpiderMonitorExt spiderMonitor = new SpiderMonitorExt();
        spiderMonitor.setTalentSubscriptionTaskId(task.getId());
        spiderMonitor.setAdminId(task.getAdminId());
        spiderMonitor.setAdminTruename(task.getAdminTruename());
        spiderMonitor.setId(IdGen.uuid());
        try {
            return insert(spiderMonitor);
        } catch (SkException e) {

        }
        return null;

    }

    /**
     * 从监听里得到数据
     *
     * @param spiderMonitor
     * @return
     */
    @Override
    public SpiderMonitorExt addLog(SpiderMonitorExt spiderMonitor) {

        SpiderMonitorExt old = null;
        try {
            String id = spiderMonitor.getId();


            if (StringUtil.isNullOrEmpty(id) || StringUtil.isNullOrEmpty(getDetailBySearch(SpiderMonitorSearchExt.getInstance().setIdIn(SkList.getInstance().addObjToList(id))))) {
                old = insert(spiderMonitor);
            }else{
                try {
                    old = getDetail(id);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }


            BeanUtils.copyPropertiesIgnoreNull(spiderMonitor, old);

            update(old);

            int status = NumberUtil.parseInt(old.getStatus());

            if (StringUtil.isNotObjEmpty(old.getTalentSubscriptionTaskId())) {

                //开始爬
                if (status == SpiderMonitorStatusEnum.INIT.getType().intValue()) {


                   SkMap<String,Object> updateMap= SkMap.getInstance().addObjToMap(TalentSubscriptionTask.STATUS,
                                    TalentSubscriptionTaskStatusEnum.RUNNING.getType())

                           ;

 //.addObjToMap(TalentSubscriptionTask.RESUME_COUNT, resumeBasicService.getCountByTalentSubscriptionTaskId(old.getTalentSubscriptionTaskId()))
                       updateMap .addObjToMap(TalentSubscriptionTask.START_RUN_TIME,old.getStartTime());

                    talentSubscriptionTaskService.updateDataById(
                            updateMap
                            , old.getTalentSubscriptionTaskId());


                } else if (

                        status == SpiderMonitorStatusEnum.STOPPED.getType().intValue()) {   //完成

                    //  .addObjToMap(TalentSubscriptionTask.RESUME_COUNT, resumeBasicService.getCountByTalentSubscriptionTaskId(old.getTalentSubscriptionTaskId()))
                    talentSubscriptionTaskService.updateDataById(
                            SkMap.getInstance()
                                    .addObjToMap(TalentSubscriptionTask.STATUS, TalentSubscriptionTaskStatusEnum.STOP.getType())
                                    .addObjToMap(TalentSubscriptionTask.STOP_TIME, new Date())


                            , old.getTalentSubscriptionTaskId());



                    //减少已运行的数量
                    adminService.reduceRunSubscriptionTask(old.getAdminId());




                }
            }


        } catch (Exception e) {

            e.printStackTrace();
        }


        return old;
    }
}